﻿using Baidu.API.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Web;

namespace Baidu.API {
    public class Dictionary {
        /// <summary>
        /// 禁止实例化
        /// </summary>
        private Dictionary() { }

        /// <summary>
        /// 自动识别中英文翻译 已知bug: errno永远是0 (API的问题) !!!!用IsSuccess检查是否出错
        /// </summary>
        /// <param name="words"></param>
        /// <returns></returns>
        public static DicResult AutoQuery(string words){
            var result = string.Empty;
            if (WordsIScn(words)){
                result = Query(words, "zh", "en");
            }
            else{
                result = Query(words, "en", "zh");
            }

            return JsonHelper.JsonDeserialize<DicResult>(result);
        }

        /// <summary>
        /// 中译英 已知bug: errno永远是0 (API的问题) !!!!用IsSuccess检查是否出错
        /// </summary>
        /// <param name="words"></param>
        /// <returns></returns>
        public static DicResult Zh2En(string words){
            var result = Query(words, "zh", "en");

            return JsonHelper.JsonDeserialize<DicResult>(result);
        }

        /// <summary>
        /// 英译中 已知bug: errno永远是0 (API的问题) !!!!用IsSuccess检查是否出错
        /// </summary>
        /// <param name="words"></param>
        /// <returns></returns>
        public static DicResult En2Zh(string words) {
            var result = Query(words, "en", "zh");
            return JsonHelper.JsonDeserialize<DicResult>(result);
        }

        /// <summary>
        /// 调用百度词典API
        /// </summary>
        /// <param name="words"></param>
        /// <param name="from">可选 zh en</param>
        /// <param name="to">可选 zh en</param>
        /// <returns></returns>
        public static string Query(string words, string from, string to) {
            const string baseUrl = "http://openapi.baidu.com/public/2.0/translate/dict/simple";
            var hc = new HttpClient();
            var queryString = new QueryString();
            queryString.Add("from", from);
            queryString.Add("to", to);
            queryString.Add("client_id", Config.API_KEY);
            queryString.Add("q", HttpUtility.UrlEncode(words));

            var response = hc.GetStringAsync(string.Format(@"{0}?{1}", baseUrl, queryString.ToString()));

            return response.Result;
        }

        /// <summary>
        /// 调用百度词典API
        /// </summary>
        /// <param name="words"></param>
        /// <param name="from">可选 zh en</param>
        /// <param name="to">可选 zh en</param>
        /// <returns></returns>
        //private static async Task<string> Query(string words, string from, string to){
        //    const string baseUrl = "http://openapi.baidu.com/public/2.0/translate/dict/simple";

        //    var hc = new HttpClient();

        //    var queryString = new QueryString();
        //    queryString.Add("from", from);
        //    queryString.Add("to", to);
        //    queryString.Add("client_id", Config.API_KEY);
        //    queryString.Add("q", HttpUtility.UrlEncode(words));

        //    var response = await hc.GetStringAsync(string.Format(@"{0}?{1}", baseUrl, queryString.ToString()));
            
        //    return response;
        //}

        /// <summary>
        /// 判断句子中是否含有中文     宁夏大学 张冬 zd4004.blog.163.com
        /// </summary>
        /// <param name="words"></param>
        /// <returns></returns>
        private static bool WordsIScn(string words) {
            string TmmP;

            for (int i = 0; i < words.Length; i++) {
                TmmP = words.Substring(i, 1);

                byte[] sarr = System.Text.Encoding.GetEncoding("gb2312").GetBytes(TmmP);

                if (sarr.Length == 2) {
                    return true;
                }
            }
            return false;
        }
    }

}
